package com.whmnx.shop.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.ShoppingBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.base.view.RoundImageView;
import com.whmnx.shop.R;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/10/19 0019 16:55
 * version: 1.0
 * @author Administrator
 */
public class ShopListAdapter extends CommonRecyclerAdapter<ShoppingBean> {

    public OnEditCall onEditCall;

    private boolean isEdit;

    private  ShoppingBean shoppingBean;


    private Context context;
    private int num;

    public ShopListAdapter(Context context) {
        super(context, R.layout.item_shop2);
        this.context = context;
    }


    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();
    }

    public void setOnEditCall(OnEditCall onEditCall) {
        this.onEditCall = onEditCall;
    }


    public void allSelect(boolean isAll) {
        for (ShoppingBean shoppingBean : getData()) {
              shoppingBean.setSelect(isAll);
              notifyDataSetChanged();
        }
        if (onEditCall != null) {
            onEditCall.heji();
        }
    }


    @Override
    public void onUpdate(final BaseAdapterHelper helper, final ShoppingBean item, final int position) {
        helper.setText(R.id.tv_goods_name, (String) item.getShopCart_ProductName());
        helper.setText(R.id.tv_guige_name, "规格: " +  item.getShopCart_SPECName());
        helper.setText(R.id.tv_moeny, "￥" + item.getShopCart_SPECMoney());
        helper.setText(R.id.tv_edit_count, item.getShopCart_Num() + "");



        if (!TextUtils.isEmpty(item.getShopCart_ProductImage())) {
            String[] split = item.getShopCart_ProductImage().split(",");
            String url = split[0];
            ImageUtil.setImage((RoundImageView)helper.getView(R.id.iv_img), url);
        }

        if (item.isSelect()) {
            helper.setImageResource(R.id.iv_select, R.mipmap.check2);
        } else {
            helper.setImageResource(R.id.iv_select, R.mipmap.check1);
        }


        helper.setOnClickListener(R.id.iv_select, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isSelect()) {
                    item.setSelect(false);
                    helper.setImageResource(R.id.iv_select, R.mipmap.check1);

                } else {
                    item.setSelect(true);
                    helper.setImageResource(R.id.iv_select, R.mipmap.check2);
                }

                if (onEditCall != null) {
                    onEditCall.isAll();
                    onEditCall.heji();
                }
            }
        });

        num = item.getShopCart_Num();

            //编辑 +
            helper.setOnClickListener(R.id.tv_jia, v -> {
                        num++;
                        item.setShopCart_Num(num);
                        editShopcart(num,item.getShopCart_Id());
                        notifyItemChanged(position);
                if (onEditCall != null){
                            onEditCall.heji();
                        }
            });

            // -
            helper.setOnClickListener(R.id.tv_jian, v -> {
                    if (item.getShopCart_Num() > 1) {
                        num--;
                        item.setShopCart_Num(num);
                        editShopcart(num,item.getShopCart_Id());
                        notifyItemChanged(position);
                        if (onEditCall != null){
                            onEditCall.heji();
                        }
                    }
            });
    }

    private void editShopcart(int num, String shopCart_id) {
        Map<String, Object> params = new HashMap<>();
        params.put("shopCart_Id",shopCart_id);
        params.put("shopCart_Num",num);
        params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));

        HttpClient.post(context, Constants.EDITSHOPCART, params,new CallBack<LoginBean>() {
            @Override
            public void onSuccess(LoginBean result) {

            }
        });
    }


    public interface OnEditCall {
        void collection(String goodsId);

        void updateSpec(int fposition, int position, String carId, String goodsId);

        void heji();

        void isAll();

        void sonSelect(int position);

        void updateData();

        //void onReceive(ShopCarBean shopCarBean);
    }

}